const getOrder = () => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'getOrder'
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve(res.result.data);
				} else {
					reject(res.result.message || '获取奖品列表失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

const addOrder = (data) => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'addOrder',
				...data
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve({
						id: res.result.id,
						message: res.result.message
					});
				} else {
					reject(res.result.message || '添加奖品失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

const updateOrder = (id, data) => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'updateOrder',
				_id: id,
				...data
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve({
						updated: res.result.updated,
						message: res.result.message
					});
				} else {
					reject(res.result.message || '更新奖品失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

const batchDeleteOrders = (ids) => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'batchDeleteOrders',
				ids: ids
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve({
						deleted: res.result.deleted,
						message: res.result.message
					});
				} else {
					reject(res.result.message || '批量删除订单失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

const deleteOrder = (id) => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'deleteOrder',
				_id: id
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve({
						deleted: res.result.deleted,
						message: res.result.message
					});
				} else {
					reject(res.result.message || '删除奖品失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

const importOrders = (orders) => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'importOrders',
				orders: orders
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve({
						imported: res.result.imported,
						message: res.result.message
					});
				} else {
					reject(res.result.message || '导入订单失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

const clearOrders = () => {
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'order',
			data: {
				action: 'clearOrders'
			},
			success: (res) => {
				if (res.result.code === 200) {
					resolve({
						deleted: res.result.deleted,
						message: res.result.message
					});
				} else {
					reject(res.result.message || '清空订单失败');
				}
			},
			fail: (err) => {
				reject('网络请求失败: ' + err.errMsg);
			}
		});
	});
};

export {
	getOrder,
	addOrder,
	updateOrder,
	deleteOrder,
	importOrders,
	clearOrders,
	batchDeleteOrders,
};